package com.hsogoo.ultraman.utils;

import com.google.common.base.Strings;
import com.hsogoo.ultraman.Constant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

/**
 * Created by za-huangsaigang on 2018/9/13.
 */
@Slf4j
public class ConfigMetaUtil {



    /**
     * 加载/META-INF/app.properties中的配置项
     * @return
     */
    public Properties loadAppProperties(){
        Properties appProperties = new Properties();
        try {
            InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(Constant.APP_PROPERTIES_CLASSPATH);
            if (inputStream == null) {
                inputStream = getClass().getResourceAsStream(Constant.APP_PROPERTIES_CLASSPATH);
            }

            if (inputStream == null) {
                log.warn("{} not found from classpath!", Constant.APP_PROPERTIES_CLASSPATH);
                return appProperties;
            }

            try {
                appProperties.load(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            } finally {
                inputStream.close();
            }
        } catch (Throwable ex) {
            log.error("Initialize DefaultApplicationProvider failed.", ex);
        }
        return appProperties;
    }

    /**
     * /META-INF/app.properties中配置的app.name作为应用的名称
     * @return
     */
    public String getAppName() {
        String appName = loadAppProperties().getProperty("app.name");
        if (Strings.isNullOrEmpty(appName)) {
            appName = Constant.NO_APP_NAME_SET;
            log.warn("appName is not set, please make sure it is set in classpath:/META-INF/app.properties, now apollo " +
                    "will only load public namespace configurations!");
        }
        return appName;
    }

    /**
     * 本地配置文件的路径
     * @return
     */
    public String getDefaultLocalCacheDir() {
        String cacheRoot = isOSWindows() ? "C:\\config-data\\%s" : "config-data/%s";
        return String.format(cacheRoot, getAppName());
    }

    /**
     * 是否windows操作系统
     * @return
     */
    public boolean isOSWindows() {
        String osName = System.getProperty("os.name");
        if (Strings.isNullOrEmpty(osName)) {
            return false;
        }
        return osName.startsWith("Windows");
    }

    public String getClassPath() {
        return "classpath";
    }

    /**
     * -Denv=local  设置本地模式
     * @return
     */
    public boolean isLocalMode() {
        if("local".equalsIgnoreCase(getEnvName())){
            return true;
        }
        return false;
    }

    /**
     * 获取当前应用的环境名
     * @return 当前应用环境名
     */
    public String getEnvName(){
        String env = System.getProperty("env");
        if(StringUtils.isNotBlank(env)){
            log.info("Environment is set to [{}] by JVM system property 'env'.", env);
            return env;
        }
        env = System.getenv("ENV");
        if(StringUtils.isNotBlank(env)){
            log.info("Environment is set to [{}] by OS env variable 'ENV'.", env);
            return env;
        }
        return env;
    }

    /**
     * 通过启动参数-Dregistry.url=127.0.0.1:2181来配置注册中心地址
     * 或者通过/META-INF/app.properties文件中配置registry.url=127.0.0.1:2181
     * @return
     */
    public String getRegistryUrl(){
        //-Dregistry.url=127.0.0.1:2181
        String registryUrl = System.getProperty("registry.url");
        if(StringUtils.isBlank(registryUrl)){
            registryUrl = loadAppProperties().getProperty("registry.url");
        }
        return registryUrl;
    }

}
